home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / JAVA_ALL / JAVA_UTL / HYPERPRO / SRC / PVS / HYPERBOL / COMPLEX.JAV < prev    next >
Encoding:
Text File  |  1996-09-14  |  3.9 KB  |  191 lines

  1. package PVS.Hyperbolic;
  2. //
  3. // Copyright (C) 1996 by Vladimir Bulatov <V.Bulatov@ic.ac.uk>.  
  4. //        All rights reserved.
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions
  8. // are met:
  9. // 1. Redistributions of source code must retain the above copyright
  10. //    notice, this list of conditions and the following disclaimer.
  11. // 2. Redistributions in binary form must reproduce the above copyright
  12. //    notice, this list of conditions and the following disclaimer in the
  13. //    documentation and/or other materials provided with the distribution.
  14. //
  15. // THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  16. // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. // ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  19. // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  21. // OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23. // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  24. // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  25. // SUCH DAMAGE.
  26.  
  27. public class Complex {
  28.   public double re,im;
  29.  
  30.   static Complex i = new Complex(0.,1.);
  31.  
  32.   public Complex(double x,double y){
  33.     re = x;
  34.     im = y;
  35.   }
  36.  
  37.   public Complex(Complex z){
  38.     this.re = z.re;
  39.     this.im = z.im;
  40.   }
  41.  
  42.   public Complex(double x){
  43.     this.re = x;
  44.     this.im = 0.;
  45.   }
  46.  
  47.   public Complex(){
  48.     this(0,0);
  49.   }
  50.  
  51.   public Complex add(Complex a,Complex b){
  52.     re = a.re + b.re;
  53.     im = a.im + b.im;
  54.     return this;
  55.   }
  56.  
  57.   public Complex add(Complex a){
  58.     re += a.re;
  59.     im += a.im;
  60.     return this;
  61.   }
  62.  
  63.   public Complex add(double a){
  64.     re += a;
  65.     return this;
  66.   }
  67.  
  68.   public Complex add(double a,double b){
  69.     re += a;
  70.     im += b;
  71.     return this;
  72.   }
  73.  
  74.   public Complex sub(Complex a){
  75.     re -= a.re;
  76.     im -= a.im;
  77.     return this;
  78.   }
  79.  
  80.   public Complex sub(Complex a, Complex b){
  81.     re = a.re - b.re;
  82.     im = a.im - b.im;
  83.     return this;
  84.   }
  85.  
  86.   public Complex conj(){
  87.     im = -im;
  88.     return this;
  89.   }
  90.  
  91.   public Complex conj(Complex a){
  92.     re = a.re;
  93.     im = -a.im;
  94.     return this;
  95.   }
  96.  
  97.   public Complex neg(){
  98.     re = -re;
  99.     im = -im;
  100.     return this;
  101.   }
  102.  
  103.   public Complex neg(Complex a){
  104.     re = -a.re;
  105.     im = -a.im;
  106.     return this;
  107.   }
  108.  
  109.   public Complex div(Complex a, Complex b){
  110.     double d = b.re*b.re+b.im*b.im;
  111.     re = (a.re*b.re+a.im*b.im)/d;
  112.     im = (a.im*b.re-a.re*b.im)/d;
  113.     return this;
  114.   }
  115.  
  116.   public Complex div(Complex a){
  117.     double d = a.re*a.re+a.im*a.im;
  118.     double t = (re*a.re+im*a.im)/d;
  119.     im = (im*a.re-re*a.im)/d;
  120.     re = t;
  121.     return this;
  122.   }
  123.  
  124.   public Complex div(double b){
  125.     im /= b;
  126.     re /= b;
  127.     return this;
  128.   }
  129.  
  130.   public Complex mul(Complex a,Complex b){
  131.     re = a.re * b.re - a.im * b.im;
  132.     im = a.re * b.im + a.im * b.re;
  133.     return this;
  134.   }
  135.  
  136.   public Complex mul(Complex a, double b){
  137.     im = a.im * b;
  138.     re = a.re * b;
  139.     return this;
  140.   }
  141.  
  142.   public Complex mul(Complex b){
  143.     double t = re * b.re - im * b.im;
  144.     im = re * b.im + im * b.re;
  145.     re = t;
  146.     return this;
  147.   }
  148.  
  149.   public Complex mul(double b){
  150.     im *= b;
  151.     re *= b;
  152.     return this;
  153.   }
  154.  
  155.   public Complex set(Complex a){
  156.     re = a.re;
  157.     im = a.im;
  158.     return this;
  159.   }
  160.  
  161.   public Complex set(double x, double y){
  162.     re = x;
  163.     im = y;
  164.     return this;
  165.   }
  166.  
  167.   public double abs2(){
  168.     return re*re+im*im;
  169.   }
  170.  
  171.   public double abs(){
  172.     return Math.sqrt(abs2());
  173.   }
  174.  
  175.   public double arg(){
  176.     return Math.atan2(im,re);
  177.   }
  178.  
  179.   public Complex normalize(){
  180.     double r = abs();
  181.     re /= r;
  182.     im /= r;
  183.     return this;
  184.   }
  185.  
  186.   public String toString(){
  187.     return "("+ String.valueOf(re) + "," + String.valueOf(im) + ")";
  188.   }
  189.  
  190. }
  191.